<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理</title>
<!-- 导入jquery核心类库 -->
<%@include file="../include/jqueryeasyui.jsp"%>

<script type="text/javascript">

$(function(){
	
	
	/* 二维数组操作
	var a = [1,2,3,4]; 
	a.push(9)
	var b = [5,6,7,8];
	var c = new Array(a,b);  
 
	alert(c[0][4])  */ 
	main();
	
	
});



	function main(){
		 getSessionStaff();
 		 getPinleiBodyItems();
 		 
 		 
		 initToolBarBtn();
 		 initCeliangBuweiWindow();
		 
	}
	

	
	window.dataArray = new Array();
	function getPinleiBodyItems(){
		
		var csinfo =  new Object();
		csinfo.field = 'csName';
		csinfo.title = '规格名称';
		csinfo.rowspan = 1;
		csinfo.width = 150;

 		window.dataArray.push(csinfo);
		
		var heightkey =  new Object();
		heightkey.title = '号';
		heightkey.field = 'heightkey';
		heightkey.rowspan = 1;
		heightkey.width = 100;
		window.dataArray.push(heightkey);
		
		var widthkey =  new Object();
		widthkey.title = '型';
		widthkey.field = 'widthkey';
		widthkey.rowspan = 1;
		widthkey.width = 100;
		window.dataArray.push(widthkey);
		
		var gender =  new Object();
		gender.title = '性别';
		gender.field = 'gender';
		gender.rowspan = 1;
		gender.width = 50;
		gender.formatter = formatterGender;
		window.dataArray.push(gender);
 		
 		
		//发送网络请求
		 $.get("${pageContext.request.contextPath}/clothesType/page.findByID.ashx?typeID="+GetQueryString('clothesTypeID'), function(result){
			 
			var nameStr = result.typeItemsName;
			var idStr = result.typeItemsStr
			
			var nameArray = nameStr.split(',')
			var idArray = idStr.split(',')
			
			
			
			 	
			 
			 
				 
			 for (var int = 0; int < idArray.length; int++) {
				var celaingbuwei =  new Object();
				celaingbuwei.field = 'bodyitem_'+idArray[int];
				celaingbuwei.title = nameArray[int];
				celaingbuwei.rowspan = 1;
				celaingbuwei.width = 80;
				window.dataArray.push(celaingbuwei);					 
			}	
 
			var sizeid =  new Object();
			sizeid.title = '操作';
			sizeid.field = 'sizeid';
			sizeid.rowspan = 1;
			sizeid.width = 100;
			sizeid.formatter = formatterID;
			window.dataArray.push(sizeid);
			
			
			
			var columns =  new Array() ;
 			//columns[0] = csinfo.push(window.dataArray);
 			columns[0] =  window.dataArray ;
			 
			initDatagrid(columns);
			 
			buildSaveOrUpdateWindowElement(window.dataArray)
 		 
		 }); 
		
		
	}
	
	//根据列表字段创建表单字段
	function buildSaveOrUpdateWindowElement(dataArray){
		for (var int = 0; int < dataArray.length; int++) {
			var field = dataArray[int];
			//生成html <tr>
			if(field.field=='csName'){
				
				$('#csTable').append(
						'<tr>'+
						'<td>规格名称:</td>'+
						'<td>'+GetQueryString('csName') 
							+'</td>'+
						
						'</tr>'		
				);
			 
			}else if(field.field=='gender'){
				$('#csTable').append(
						'<tr>'+
						'<td>性别</td>'+
						'<td><select style="width:168.44px;" id ="'+field.field+'" name="'+field.field+'"> <option value ="0" selected = "selected">男</option> <option value ="1">女</option></select></td>'+
						'</tr>'		
				);
				
			}else if(field.field=='sizeid'){
				
			}else{
				$('#csTable').append(
						'<tr>'+
						'<td>'+field.title+'</td>'+
						'<td><input name="'+field.field+'" id="'+field.field+'" type="text" class="easyui-validatebox"/></td>'+
						'</tr>'
					)
			}
			
		
			
			
			
		}
		
	}
	

 	
	function formatterGender(value,row,index){
		if(value==false){
			return '男'
		}
		 
		return "女";

}
 
 
	
	function  getSessionStaff(){
		var staff = '<%=session.getAttribute("staff")%>';
		window.staff =  JSON.parse(staff).sLoginName;
	}
	function GetQueryString(name)
	{
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  decodeURI(r[2]); return null;
	}



	function doAdd(){
	
		$('#celiangbuweiWindow').form('clear');
		$('#celiangbuweiWindow *').trigger('blur');// 离焦
		$('#sizeid').val("-1"); 
 		$('#csid').val(GetQueryString("csID")); 
		
		$('#celiangbuweiWindow').window("open").window('setTitle', '成衣管理');
	}
	
	function doEdit(){
		
		 
  		
		
		var rowData = $('#grid').datagrid('getSelected');  
		//alert(JSON.stringify(rowData))
		 log(JSON.stringify(rowData))
        
        if (rowData == null) {  
            $.messager.alert("提示", "请选择要修改的行！", "info");  
        } else{
         
        	
        	//回显
        	for(var item in rowData){
        	 	
       	 		$('#'+item).val(rowData[item]);
       	 		if(item=="gender"){
           	 		console.log(1)
           	 		if(rowData[item]==true){
           	 			$('#'+item).val(1);
           	 		}else if(rowData[item]==false){
           	 			$('#'+item).val(0);
           	 		}
           	 		
           	 	} 
        		
        	}
        	
        	
  
			// 弹出窗口
			  
			$("#celiangbuweiWindow").window('open').window('setTitle', '成衣修改');
        } 
	}
	
	
	 //增加查询参数，重新加载表格  
    function reloadgrid() {  
  
        //查询参数直接添加在queryParams中      
        var queryParams = $('#grid').datagrid('options').queryParams;  
        queryParams.sizekey = $('#searchcontent').val();  
        
        $('#grid').datagrid('options').queryParams = queryParams;  
        $("#grid").datagrid('load');  
    }  
	 
	function doSearch(){
		reloadgrid();
	}
	
 
	
 

	
	
	function formatterID(value,row,index){
		if(window.staff=='admin'){

			return "<a onclick=doDel("+index+") href=#>删除</a>";
		}else{
			return "";
		}
	}
	 
	 
	
 
	function doDel(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
		log(111)
		     $.messager.confirm("操作提示", "确认删除 当前记录？", function (data) {  
		            if (data) {  
		            	$.post("${pageContext.request.contextPath}/hodo_clothes_size/page.delDetail.ashx?sizeid="+row.sizeid,function(data){
			    			
			    			if(data == "success"){
			    				// 成功
			    				$.messager.alert('消息','操作成功 ','info');
			    				reloadgrid();
			    				
			    			
			    			}else{
			    					$.messager.alert('消息','<br>操作失败','info');
			    				
			    			}
			    				
			    		}); 
		            }  
		            else {  
		                 
		            }  
		        }); 


		}
	}
	function doProcess(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
/* 	    	alert(row.bfId)
 */			location.href="${pageContext.request.contextPath}/page.bfdetails.ashx?bfid="+row.bfid;

		}
	}
	
	function formatterDate(value,row,index){
		if(value==''||null==value){
			return '';
		}
		var now = new Date(value);  
		var year=now.getYear()+1900; 
		var month=now.getMonth()+1; 
		var date=now.getDate(); 
		var hour=now.getHours(); 
		var minute=now.getMinutes();
		minute= (minute > 9) ? minute=minute : minute='0'+minute;
		//var second=now.getSeconds(); 
		return year+"-"+month+"-"+date+" "+hour+":"+minute; 
	}
	
	 function formaterbfIsDone(value,row,index){
		if(value==false){
			return "否";
		}else{
			return "是";
		}
	} 
	


	 

	function initToolBarBtn(){
		$('#add').click(doAdd);
		$('#edit').click(doEdit);
		$('#search').click(doSearch);

	}
	function initDatagrid(columns){
 		// 先将body隐藏，再显示，不会出现页面刷新效果
		$("body").css({visibility:"visible"});
		
		
		// 收派标准数据表格
		$('#grid').datagrid( {
			iconCls : 'icon-forward',
			fit : true,
			border : false,
			rownumbers : true,
			singleSelect:true,
			striped : true,
			toolbar : '#tb',
			url : "${pageContext.request.contextPath}/hodo_clothes_size/page.detaillist.ashx?csID="+GetQueryString("csID")+"&type="+GetQueryString("type"),
			idField : 'bfId',
			columns : columns,
			pageList: [100,200,300],
			pagination:true,
			view:myview,
			emptyMsg:"<font size=5>暂无记录</font>"
			
		});
		
	}

	
	//初始化窗口
	function  initCeliangBuweiWindow(){
	
		$('#celiangbuweiWindow').window({
			
            title: '成衣管理',
            width: 500,
            modal: true,
            shadow: true,
            closed: true,
            height: 666,
            resizable:true,
            closable:true
            
        });
		
		//提交表单
		$('#save').click(function(){
				// 校验
				if($('#sform').form('validate')){
					// 1 、获得form 数据
					//var params = $('#sform').serialize() ;
					var params = $('#sform').serializeObject();
 					// 2、将数据发送给服务器 
					$.post("${pageContext.request.contextPath}/hodo_clothes_size/page.saveOrUpdateDetail.ashx?params="+JSON.stringify(params)  , function(data){
						if(data == "success"){
							// 成功
							$.messager.alert('消息','操作成功 ','info');
							// 关闭窗口
							$('#celiangbuweiWindow').window('close');
							// 清除表单数据
							$('#celiangbuweiWindow').form('clear');
							$('#celiangbuweiWindow *').trigger('blur');// 离焦
							
							// 刷新表格数据 
							$('#grid').datagrid('reload');
						}else if(data=="conflict"){
							$.messager.alert('消息','名称被占用。 ','info');
						}else{
							$.messager.alert('消息','操作失败 ','info');
						}
							
					});
				}
				
			});	
	}
	
	
	
	var myview = $.extend({},$.fn.datagrid.defaults.view,{
		onAfterRender:function(target){
			$.fn.datagrid.defaults.view.onAfterRender.call(this,target);
			var opts = $(target).datagrid('options');
			var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
			vc.children('div.datagrid-empty').remove();
			if (!$(target).datagrid('getRows').length){
				var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || 'no records').appendTo(vc);
				d.css({
					position:'absolute',
					left:0,
					top:50,
					width:'100%',
					textAlign:'center'
				});
			}
		}
	});

	
</script>	
</head>
<body class="easyui-layout" >
	<div region="center">
	
		<table id="grid">
		
		</table>
	</div>
	
	<div class="easyui-window"  id="celiangbuweiWindow" collapsible="false" minimizable="false" maximizable="false" style="top:50px;left:200px">
		<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		<div region="center" style="overflow:auto;padding:5px;" border="false">
			<form id="sform">
				<table id="csTable" class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">
							成衣管理
								<input id="sizeid" name="sizeid" type="hidden"/> 
								<input id="csid" name="csid" type="hidden"/>
 </td>
					</tr>
					
					
					 
					
			 
					
					
					
					
				
					</table>
			</form>
		</div>
	</div>


	
		<div id="tb" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" id="add" class="easyui-linkbutton" iconCls="icon-add" plain="true" >增加</a>
			<a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" >编辑</a>
 
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;规格名称:&nbsp;&nbsp;&nbsp;&nbsp;<input id="searchcontent" class="easyui-textbox" style="width:80px"/>
			&nbsp;&nbsp;&nbsp;&nbsp;<a href="#"  id="search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
		
		</div>
	</div>

	
</body>
</html>